package JsonInterface.test.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        int port = 40001;
         AtomicInteger a  = new AtomicInteger(0);
        try (ServerSocket serverSocket = new ServerSocket(port)) {
            while (true) {
                Socket clientSocket = serverSocket.accept();
                new Thread(() -> {
                    try {
                        System.out.println("客户端连接: " + clientSocket.getInetAddress().getHostAddress());
                        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                        a.getAndIncrement();
                        out.println("111");
                        // 关闭连接
                        //clientSocket.close();
                    } catch (IOException e) {
                        System.err.println("处理客户端异常: " + e.getMessage());
                    }
                }).start();
                System.out.println(a.get());
            }
        } catch (IOException e) {
            System.err.println("服务器异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
